package com.jiyun.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * 全局安全配置
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserDetailsService uacUserDetailService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //TODO 必须记住同学们
        auth.userDetailsService(uacUserDetailService).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //token接口拦截通过httpBasic进行认证
        http.authorizeRequests().antMatchers("/oauth/token").fullyAuthenticated().and().httpBasic()
                //支持跨域访问
                .and().cors()
                //关闭防范csrf攻击
                .and().csrf().disable();
        //TODO 所有的接口需要进行认证
        http.authorizeRequests().anyRequest().authenticated();
    }

}
